package com.example.chatchatbackend.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.chatchatbackend.entity.RestBean;
import com.example.chatchatbackend.entity.dto.Category;

import com.example.chatchatbackend.entity.vo.response.CategoryVO;
import com.example.chatchatbackend.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 丁真
 * @since 2024-10-28 05:03:47
 */
@Slf4j
@RestController
@RequestMapping("/api/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping
    public RestBean<List<Category>> getAll(){
        List<Category> categoryList = categoryService.getAllCategory();
        log.info("getAll-Buy:{}",categoryList);
        return RestBean.success(categoryList);
    }

    @PostMapping
    public RestBean<CategoryVO> insertCategory(@RequestBody CategoryVO categoryListVO){
        CategoryVO insertCategoryVO = categoryService.insertCategory(categoryListVO);
        return RestBean.success(insertCategoryVO);

    }

    @PostMapping("/list")
    public RestBean<List<CategoryVO>> insertCategoryList (@RequestBody List<CategoryVO> categoryListVO){
        List<CategoryVO> CategoryList = categoryService.insertCategoryList(categoryListVO);
        return RestBean.success(CategoryList);
    }

    @GetMapping("/pageThree")
    public RestBean<IPage<Category>> pageThree(@RequestParam(value = "pageNum" ,defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        return RestBean.success(categoryService.pageThree(pageNum, pageSize));
    }

    @GetMapping("/ids")
    public RestBean<List<CategoryVO>> findByIds(@RequestBody List<Integer> ids){
        List<CategoryVO> categoryList = categoryService.findByIds(ids);
        return RestBean.success(categoryList);
    }

    @GetMapping("/name")
    public RestBean<List<CategoryVO>> findByTitle(@RequestBody String title ){
        List<CategoryVO> categoryVOList = categoryService.findByTitle(title);
        return RestBean.success(categoryVOList);
    }

}
